#include "game2.h"
#include "ui_game2.h"
#include <QPainter>
#include <QDebug>
#include <QTimer>
#include <QMouseEvent>

map3 map33;
QTimer *timer3=new QTimer();
int speed3=500;

Game2::Game2(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Game2)
{
    ui->setupUi(this);
    connect(timer3,&QTimer::timeout,this,&Game2::timerUpdate);
    timer3->stop();
}

Game2::~Game2()
{
    delete ui;
}

void Game2::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    //设置网格颜色
    QPen pen(Qt::gray);
    painter.setPen(pen);

    //以label左上角为原点
    painter.translate(ui->label->x(),ui->label->y());

    //清空画布
       QBrush brush(Qt::white,Qt::SolidPattern);
       painter.setBrush(brush);
       painter.drawRect(0,0,ui->label->width()-1,ui->label->height()-1);

    //绘制网格
    for(int x=0;x<=map33.getwidthnum();x++)
    {
        painter.drawLine(x*map33.getwidth(),0,x*map33.getwidth(),map33.getheightnum()*map33.getheight());
    }
    for(int y=0;y<=map33.getheightnum();y++)
    {
        painter.drawLine(0,y*map33.getheight(),map33.getwidthnum()*map33.getwidth(),y*map33.getheight());
    }

    //绘制细胞
    brush.setColor(Qt::black);
    painter.setBrush(brush);
    for(int x=0;x<map33.getwidthnum();x++)
    {
        for(int y=0;y<map33.getheightnum();y++)
        {
            if(map33.getmap(x,y)==1)
                    painter.drawRect(x*map33.getwidth(),y*map33.getheight(),map33.getwidth(),map33.getheight());
        }
    }
    painter.end();
}

void Game2::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        QPoint point=event->pos()-ui->label->pos();
        //offset=event->pos()-ui->label->pos();
        if(point.x()>=0 and point.x()<=map33.getwidth()*map33.getwidthnum() and point.y()>=0 and point.y()<=map33.getheight()*map33.getheightnum())//防止超出范围
        {
            lastcellx=point.x()/map33.getwidth();
            lastcelly=point.y()/map33.getheight();
            map33.changemap(point.x()/map33.getwidth(),point.y()/map33.getheight());//改变细胞状态
            update();//重绘界面
        }
    }
}

void Game2::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        QPoint point=event->pos()-ui->label->pos();
        //offset=event->pos()-ui->label->pos();
        if(point.x()>=0 and point.x()<=map33.getwidth()*map33.getwidthnum() and point.y()>=0 and point.y()<=map33.getheight()*map33.getheightnum())//防止超出map范围
        {
            if(lastcellx!=point.x()/map33.getwidth() or lastcelly!=point.y()/map33.getheight())
            {
                lastcellx=point.x()/map33.getwidth();
                lastcelly=point.y()/map33.getheight();
                map33.changemap(point.x()/map33.getwidth(),point.y()/map33.getheight());
                update();//重绘界面
            }
        }
    }
}

void Game2::timerUpdate()
{
    map33.mapupdate();
    update();//更新界面
}

void Game2::on_startpushButton_clicked()
{
    timer3->start(speed3);
}

void Game2::on_pausepushButton_clicked()
{
    timer3->stop();
}

void Game2::on_clearpushButton_clicked()
{
    map33.mapclear();
    timer3->stop();
    update();
}

void Game2::on_backpushButton_clicked()
{
    this->close();
    acquaintance *a=new acquaintance();
    a->show();
}

void Game2::on_tippushButton_clicked()
{
    this->close();
    tip *t=new tip();
    t->show();
}

void map3::mapupdate()
{
    int dx[8]={0,0,-1,1,-1,1,-1,1};
    int dy[8]={1,-1,0,0,1,1,-1,-1};

    memset(neighbor,0,sizeof(neighbor));//细胞清零

    //统计邻居细胞数量
    for(int x=0;x<widthnum;x++)
    {
        for(int y=0;y<heightnum;y++)
        {
            if(map[x][y]==1)
            {
                for(int i=0;i<8;i++)
                {
                    if(dx[i]+x>=0 and dx[i]+x<=widthnum and dy[i]+y>=0 and dy[i]+y<=heightnum) //防止越界
                    {
                        neighbor[dx[i]+x][dy[i]+y]++;//
                    }
                }
            }
        }
    }

    //改变细胞状态
    for(int x=0;x<widthnum;x++)
    {
        for(int y=0;y<heightnum;y++)
        {
            if(neighbor[x][y]==3)
                map[x][y]=1;
            else if(neighbor[x][y]!=2)
                map[x][y]=0;
        }
    }
}

void map3::mapclear()
{
    for(int x=0;x<widthnum;x++)
    {
        for(int y=0;y<heightnum;y++)
        {
            map[x][y]=0;
        }
    }
}
